package ro.sep.web.utils;

import java.util.ArrayList;
import java.util.List;

import ro.sep.web.dao.IProductDao;
import ro.sep.web.services.model.SearchResult;
import ro.sep.web.services.model.SearchSiteResult;

public class SearchSiteEngine extends Thread {

	private IHasSiteResults siteResults;
	private IProductDao productDao;
	private String text;
	private String siteName;
	private String urlToSite;
	
	public SearchSiteEngine(IHasSiteResults siteResults,
			IProductDao productDao, String text, String siteName, String urlToSite) {
		this.siteResults = siteResults;
		this.productDao = productDao;
		this.text = text;
		this.siteName = siteName;
		this.urlToSite = urlToSite;
	}
	
	@Override
	public void run() {
		String[] words = text.split(" ");
		List<SearchResult> searchResults = new ArrayList<SearchResult>();
		for (String word : words) {
			for (SearchResult searchResult : productDao.getSearchResult(
					word, siteName)) {
				searchResults.add(searchResult); 
			}
		}
		SearchSiteResult searchSiteResult = new SearchSiteResult();
		searchSiteResult.setSiteName(siteName);
		searchSiteResult.setUrlToSite(urlToSite);
		searchSiteResult.setListOfResults(searchResults);
		siteResults.addSiteResult(searchSiteResult);
	}
}
